
/*
    Software License

    Copyright (C) 2021-05-24  Xoronos

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, version 3.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

/*
    Liabilities

    The software is provided "AS IS" without any warranty of any kind, either expressed,
    implied, or statutory, including, but not limited to, any warranty that the software
    will conform to specifications, any implied warranties of merchantability, fitness
    for a particular purpose, and freedom from infringement, and any warranty that the
    documentation will conform to the software, or any warranty that the software will
    be error free.

    In no event shall Xoronos be liable for any damages, including, but not limited to,
    direct, indirect, special or consequential damages, arising out of, resulting from,
    or in any way connected with this software, whether or not based upon warranty,
    contract, tort, or otherwise, whether or not injury was sustained by persons or
    property or otherwise, and whether or not loss was sustained from, or arose out of
    the results of, or use of, the software or services provided hereunder.

    To request the provided software under a different license you can contact us at
    support@xoronos.com
*/

#include <stdio.h>
#include "examples.h"
#include "xrn_crypto.h"

    /////////////////////////////////////////////////////
    //                   Introduction                  //
    /////////////////////////////////////////////////////
    // This code example shows how to permute          //
    // with a key the compressed Xoron matrices,       // 
    // which are used to derive decompressed Xoron     // 
    // matrices.                                       //
    //                                                 //
    // In modular cryptography users are encoraged to  //
    // mine Xoron matrices to privately perform        //
    // non-standard cryptography.                      //
    /////////////////////////////////////////////////////

    /////////////////////////////////////////////////////
    //                   Pre-requisits                 //
    /////////////////////////////////////////////////////
    // The Xoron matrix compressed data structure      //
    // needs to be mined before running this code      //
    // example. Some examples on how to mine Xoron     //
    // matrices can be found in                        //
    // generate_xrn_matrix_all_rnd.c or                //
    // generate_xrn_matrix_one_third.c                 //
    //                                                 //
    // A monomial key. An example on how to generate   //
    // keys can be found in                            //
    // generate_keys_with_wrapper_functions.c          //
    /////////////////////////////////////////////////////

int permute_xrn_matrix_compressed_key(
 )
{

    //////////////////////////
    // Variable definitions //
    //////////////////////////

    FILE *input_compressed_xmatrix_fp;
    FILE *output_compressed_xmatrix_fp;
    FILE *decompressed_xmatrix_fp;
    FILE *monomial_key_fp;

    // library run time settings

    xrn_crypto_extra_settings_t settings;

    // all the subfields of settings, can be found in xrn_crypto_extra_settings_t.txt

    /////////////
    // Logging //
    /////////////

    // Set the default logging file streams
    // errors -> stderr
    // warnings -> stdout
    // notifications -> stdout
    // debug -> stdout

    xrn_set_default_log(  );

    // To change the default logging you can use the functions below,
    // where fp is the opened file pointer
    // 
    // xrn_change_error_log( fp )
    // xrn_change_warning_log( fp )
    // xrn_change_notification_log( fp )
    // xrn_change_debug_log( fp )

    //////////////////////////
    // Initialize variables //
    //////////////////////////

    // Initialize default run time settings

    if ( XSUCCESS != xrn_load_default_settings( &settings ) ) {
        return -1;
    }

    /////////////////////////////
    // Variable initialization //
    /////////////////////////////

    monomial_key_fp = fopen( "xmonomial_key.xf", "rb" );
    input_compressed_xmatrix_fp = fopen( "xrn_matrix_compressed.xmc", "rb" );
    output_compressed_xmatrix_fp = fopen( "xrn_matrix_compressed_permuted.xmc", "wb" );
    decompressed_xmatrix_fp = fopen( "xrn_matrix.xm", "rb" );

    if ( ( input_compressed_xmatrix_fp == NULL ) || ( decompressed_xmatrix_fp == NULL )
         || ( monomial_key_fp == NULL ) || ( output_compressed_xmatrix_fp == NULL ) ) {
        return -1;
    }

    /////////////////////////////////////
    // Permute Xoron matrix compressed //
    /////////////////////////////////////

    printf( "key permute... \n" );
    if ( XSUCCESS != xrn_permute_matrix_compressed_key_wrapper( input_compressed_xmatrix_fp,    // input Xoron matrix compressed file
                                                                decompressed_xmatrix_fp,        // the Xoron matrix decompressed file
                                                                monomial_key_fp,        // the Xoron matrix decompressed file
                                                                output_compressed_xmatrix_fp,   // output Xoron matrix compressed file
                                                                settings        // runtime settings
          ) ) {
        fclose( monomial_key_fp );
        fclose( input_compressed_xmatrix_fp );
        fclose( output_compressed_xmatrix_fp );
        fclose( decompressed_xmatrix_fp );
        return -1;
    }

    if ( ( 0 != fclose( input_compressed_xmatrix_fp ) ) || ( 0 != fclose( monomial_key_fp ) )
         || ( 0 != fclose( decompressed_xmatrix_fp ) ) || ( 0 != fclose( output_compressed_xmatrix_fp ) ) ) {
        return -1;
    }

    // An alternative way to perform the same computation can be achieved using the xrn_permute_matrix_compressed_key function
    // if ( XSUCCESS != xrn_permute_matrix_compressed_key ( input_compressed_xmatrix, // the xrn_matrix_compressed is of the type ( xrn_matrix_compressed_t *)
    //                                                      xrn_matrix, // the xrn_matrix is of the type ( xrn_matrix_t * )
    //                                                      monomial_key, // the monomial_key is of the type ( xmonomial_key_t )
    //                                                      output_compressed_xmatrix, // the xrn_matrix is of the type ( xrn_matrix_compressed_t * )
    //                                                      settings, 
    //       ) ) {

    printf( "Xoron matrix compressed successfully permuted\n" );
    return 0;

}

